package com.chinasoft.goodsManager.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        String uri = request.getRequestURI();
        if (uri.contains("login.html")||uri.contains("verifyCodeServlet")||uri.contains("login")||uri.contains("js")||uri.contains("fonts")||uri.contains("assets")){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            Object manager = request.getSession().getAttribute("manager");
            if (manager!=null){
                //登陆过
                filterChain.doFilter(servletRequest,servletResponse);
            }else {
                response.sendRedirect("login.html");
            }
        }
    }

    @Override
    public void destroy() {

    }
}
